# Handles inputing strings using Aerosol events.
class StringInput
	# Becomes the return value when the user presses enter, nil
	# otherwise.
	attr_reader :final

	# Current user input.
	attr_accessor :str
	
	BACKSPACE = AS::Event::KeyboardEvent::Keysyms[:Key_BACKSPACE]
	RETURN = AS::Event::KeyboardEvent::Keysyms[:Key_RETURN]
	LSHIFT = AS::Event::KeyboardEvent::Keysyms[:Key_LSHIFT]
	RSHIFT = AS::Event::KeyboardEvent::Keysyms[:Key_RSHIFT]
	
	down = %q{abcdefghijklmnopqrstuvwxyz`1234567890-=[]\;',./ }.unpack("c*") #`
	up =   %q[ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|:"<>? ].unpack("c*") #"
	UPCASE = Hash[*down.zip(up).flatten]
	
	def initialize
		@str = ""
	end
	
	def shift_char(char)
		return '' unless UPCASE.has_key?(char)
		if AS::Event.key_value(LSHIFT) or AS::Event.key_value(RSHIFT)
			return UPCASE[char].chr
		else
			return char.chr
		end
	end

	private :shift_char
	
	# Pass an event.
	def input(event_id, value)
		return unless AS::Event.key?(event_id) and value 
		
		case char = event_id>>4
		when BACKSPACE: @str.chop!
		when RETURN: @final = @str
		else @str += shift_char(char)
		end
	end
end
